84B - Magical Array - CodeForces Solution


combinatorics implementation *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lp(i,n) for(ll i = 0; i < n; i++)
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define invrep(i,a,b) for(int i = a; i >= b; i--)
#define pb(a) push_back(a)
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define line cout<<endl
#define EPS 0.0000000001
#define PI 3.14159265358979323846
#define DEC greater<int>()
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<ii> vii;
void eyle(bool value){cout<<(value?"YES":"NO")<<"\n";}
template <typename T>void out(vector<T> v){for(auto x:v)cout << x << " ";line;}
ll sum_n(ll n){return (ll)n*(n+1)/2;}
double my_log(int base, ll N){return (double)log(N)/log(base);}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll gcd(vi v){ll g = v[0];rep(i,1,sz(v)-1)g = gcd(g,v[i]);return g;}
ll lcm(int a,int b){return (ll)a*b / gcd(a,b);}
ll lcm(vi v){ll ans = v[0];rep(i,1,sz(v)-1)ans = ((ll)((v[i]*ans))/(gcd(v[i],ans)));return ans;}
//choose k from n:
ll C_kn(ll k, ll n){ll res = 1;if(k>n-k)k=n-k;for(ll i=0;i<k;++i){res*=(n-i);res/=(i+1);}return res;}
int cpd(long double a,long double b){return fabs(a-b)<=EPS?0:a-b>0?1:2;}
const int INF = INT_MAX;
ll MOD = 998244353;
const int N = 200000+7;
int ctoi(char c){
    return c - '0';
}
void solve(int _){
    int n;
    cin >> n;
    vi v(n);
    lp(i,n)cin >> v[i];
    ll ans = 0;
    int el = v[0], left = 0 ;
    rep(right,1,n-1){
        if(v[right] != el){
            ans+=sum_n(right-left);
            el = v[right];
            left = right;
        }
    }
    ans+=sum_n(n - left);
    cout << ans << endl;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t = 1;
  //  cin>>t;
    lp(i,t)solve(i+1);
    return 0;
}


Comments

Submit
0 Comments
More Questions

Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array